package wqh.controller.api;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.AlarmInComming;
import wqh.domain.Device;

import wqh.global.GlobalDevice;

import wqh.pojo.IPConver;
import wqh.pojo.MapRet;
import wqh.service.AlarmInCommingService;
import wqh.service.DeviceService;
import wqh.pojo.api.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by devts on 2016/12/7.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/tpson_api/")
public class HkApi {

    @Autowired
    private AlarmInCommingService alarmInCommingService;

    @Autowired
    private DeviceService deviceService;

    @RequestMapping(value = "/transmission_device/getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        List<Device> deviceList = this.deviceService.findAllRecord();
        if(deviceList.size() == 0) return MapRet.DATA(null);
        List<DeviceAPIpojo> deviceJsonPojoList = new ArrayList<DeviceAPIpojo>();
        for(Device device : deviceList)
        {
            if(GlobalDevice.exist(IPConver.int2Ip(device.getIp())))
            {
                device.setStatus((byte)1);
            }
            else
            {
                device.setStatus((byte)0);
            }
            deviceJsonPojoList.add(new DeviceAPIpojo(device));
        }
        return MapRet.DATA(deviceJsonPojoList);
    }

    @RequestMapping(value = "/alarm/getAll",method = RequestMethod.GET)
    public Object getAllAlarm()
    {
        List<AlarmInComming> alarmInCommingList = alarmInCommingService.findAllRecord();
        if(alarmInCommingList.size() == 0) return MapRet.DATA(null);
        List<AlarmIncommingAPIpojo> alarmIncommingAPIpojos = new ArrayList<AlarmIncommingAPIpojo>();
        for(AlarmInComming a : alarmInCommingList){
            alarmIncommingAPIpojos.add(new AlarmIncommingAPIpojo(a));
        }
        return MapRet.DATA(alarmIncommingAPIpojos);
    }

    @RequestMapping(value = "/alarm/confirmOneById",method = RequestMethod.GET)
    public Object confirmOneById(@RequestParam(value = "id",required = true) int id)
    {
        if(alarmInCommingService.deleteRecord(id) == 0){
            return MapRet.ERROR("ID字段错误。");
        }else{
            return MapRet.DATA("确认成功。");
        }
    }

    @RequestMapping(value = "/alarm/confirmAll",method = RequestMethod.GET)
    public Object confirmAll()
    {
        alarmInCommingService.deleteAll();
        return MapRet.DATA("确认成功。");
    }

    @RequestMapping(value = "/alarm/roundTest",method = RequestMethod.GET)
    public Object roundTest()
    {
        List<Device> deviceList = this.deviceService.findAllRecord();
        if(deviceList.size() == 0)  return MapRet.ERROR("请先录入用户信息传输装置。");
        for(Device device : deviceList){
            AlarmInComming a = new AlarmInComming();
            a.setIp(device.getIp());
            a.setUp_x("上传建筑消防设施部件运行状态");
            a.setType("火灾报警系统（类型1）");
            a.setAddr(0);
            a.setDev_type("通用");
            Random random = new Random();
            a.setDev_addr(random.nextInt(10000));
            a.setDev_status("火警");
            a.setAlarm_time((int)(System.currentTimeMillis() / 1000));
            alarmInCommingService.insertRecord(a);
        }
        return MapRet.DATA("插入火警测试数据成功。");
    }

}
